package com.example.it.recycleviewpager;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by 鲁迅认识的那只猹 on 9/25/2017 10:58 AM.
 * Emial 1258730808@qq.com
 * Desc:
 */

public abstract class BaseRecycleViewPagerAdapter<T> extends PagerAdapter {
    private Context context;
    private List<T> dataSource;

    public BaseRecycleViewPagerAdapter(Context context, List<T> dataSource) {
        this.context = context;
        this.dataSource = dataSource;

        //为了无线循环添加两个冗余项目
        T t1 = dataSource.get(0);
        T t2 = dataSource.get(dataSource.size() - 1);
        this.dataSource.add(dataSource.size() , t1);
        this.dataSource.add(0, t2);
    }

    /**
     * 返回视图的数量
     */
    @Override
    public int getCount() {
        return dataSource.size();
    }

    /**
     * 实例化，Page
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = bindingView(position);
        container.addView(view);
        return view;
    }

    /***
     * 抽象方法，绑定视图，需要子类实现
     */
    abstract View bindingView(int position);

    /**
     * 回收资源
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    public Context getContext() {
        return context;
    }

    public List<T> getDataSource() {
        return dataSource;
    }
}
